
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "GRTParamLightSpot.h"

#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>
#include <QGroupBox>

#include "GRTSpotLight.h"
#include "GRTSpotAt.h"
#include "GRTScene.h"
#include "GRTCommonParameter.h"
#include "GRTMemoryLeak.h"
//constructor
GRTParamLightSpot::GRTParamLightSpot(QWidget* parent) : GRTBaseParamWidget(parent), _spotLight(0), _atElement(0)
{
	//Title
	_title = new QLabel("SPOT LIGHT");

	//light position
	QGroupBox* groupPosition = new QGroupBox("Position");
	_positionXLabel = new QLabel("x=");
	_positionYLabel = new QLabel("y=");
	_positionZLabel = new QLabel("z=");
	_positionXLineEdit = new QLineEdit();
	_positionYLineEdit = new QLineEdit();
	_positionZLineEdit = new QLineEdit();
	QGridLayout* positionLayout = new QGridLayout();
	positionLayout->addWidget(_positionXLabel, 0, 0, 1, 1);
	positionLayout->addWidget(_positionXLineEdit, 0, 1, 1, 1);
	positionLayout->addWidget(_positionYLabel, 1, 0, 1, 1);
	positionLayout->addWidget(_positionYLineEdit, 1, 1, 1, 1);
	positionLayout->addWidget(_positionZLabel, 2, 0, 1, 1);
	positionLayout->addWidget(_positionZLineEdit, 2, 1, 1, 1);
	groupPosition->setLayout(positionLayout);

	//at position
	QGroupBox* groupAt = new QGroupBox("At");
	_atXLabel = new QLabel("x=");
	_atYLabel = new QLabel("y=");
	_atZLabel = new QLabel("z=");
	_atXLineEdit = new QLineEdit();
	_atYLineEdit = new QLineEdit();
	_atZLineEdit = new QLineEdit();
	QGridLayout* atLayout = new QGridLayout();
	atLayout->addWidget(_atXLabel, 0, 0, 1, 1);
	atLayout->addWidget(_atXLineEdit, 0, 1, 1, 1);
	atLayout->addWidget(_atYLabel, 1, 0, 1, 1);
	atLayout->addWidget(_atYLineEdit, 1, 1, 1, 1);
	atLayout->addWidget(_atZLabel, 2, 0, 1, 1);
	atLayout->addWidget(_atZLineEdit, 2, 1, 1, 1);
	groupAt->setLayout(atLayout);

	//light direction
	_dirXLabel = new QLabel("x=");
	_dirYLabel = new QLabel("y=");
	_dirZLabel = new QLabel("z=");
	_dirXLineEdit = new QLineEdit();
	_dirYLineEdit = new QLineEdit();
	_dirZLineEdit = new QLineEdit();
	QGridLayout* dirLayout = new QGridLayout();
	dirLayout->addWidget(_dirXLabel, 0, 0, 1, 1);
	dirLayout->addWidget(_dirXLineEdit, 0, 1, 1, 1);
	dirLayout->addWidget(_dirYLabel, 1, 0, 1, 1);
	dirLayout->addWidget(_dirYLineEdit, 1, 1, 1, 1);
	dirLayout->addWidget(_dirZLabel, 2, 0, 1, 1);
	dirLayout->addWidget(_dirZLineEdit, 2, 1, 1, 1);
	QGroupBox* groupDir = new QGroupBox("Direction");
	groupDir->setLayout(dirLayout);

	
	//constant attenuation
	_labelConstantAttenuation = new QLabel("Constant : ");
	_constantAttenuationLineEdit = new QLineEdit();

	//linear attenuation
	_labelLinearAttenuation = new QLabel("Linear : ");
	_linearAttenuationLineEdit = new QLineEdit();

	//quadratic attenuation
	_labelQuadraticAttenuation = new QLabel("Quadratic : ");
	_quadraticAttenuationLineEdit = new QLineEdit();
	
	//Attenuation
	QGridLayout* attenuationLayout = new QGridLayout();
	attenuationLayout->addWidget(_labelConstantAttenuation, 0, 0, 1, 1);
	attenuationLayout->addWidget(_constantAttenuationLineEdit, 0, 1, 1, 1);
	attenuationLayout->addWidget(_labelLinearAttenuation, 1, 0, 1, 1);
	attenuationLayout->addWidget(_linearAttenuationLineEdit, 1, 1, 1, 1);
	attenuationLayout->addWidget(_labelQuadraticAttenuation, 2, 0, 1, 1);
	attenuationLayout->addWidget(_quadraticAttenuationLineEdit, 2, 1, 1, 1);
	QGroupBox* groupAttenuation = new QGroupBox("Attenuation");
	groupAttenuation->setLayout(attenuationLayout);


	//cutoff
	_labelCutOff = new QLabel("Cut off : ");
	_cutOffLineEdit = new QLineEdit();

	//exponent
	_labelExponent = new QLabel("Exponent : ");
	_exponentLineEdit = new QLineEdit();

	//widget's layout
	_layout = new QGridLayout();

	_layout->addWidget(_title, 0, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(groupPosition, 1, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(groupAt, 2, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(groupDir, 3, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(groupAttenuation, 4, 0, 1, 2, Qt::AlignHCenter);

	_layout->addWidget(_labelCutOff, 5, 0, 1, 1, Qt::AlignHCenter);
	_layout->addWidget(_cutOffLineEdit, 5, 1, 1, 1, Qt::AlignHCenter);
	_layout->addWidget(_labelExponent, 6, 0, 1, 1, Qt::AlignHCenter);
	_layout->addWidget(_exponentLineEdit, 6, 1, 1, 1, Qt::AlignHCenter);
	_layout->addWidget(new QWidget(), 7, 0, 1, 2);
	_layout->setRowStretch(7, 1);

	setLayout(_layout);

	//connect position
	connect(_positionXLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectPosition()));
	connect(_positionYLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectPosition()));
	connect(_positionZLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setObjectPosition()));

	//connect at
	connect(_atXLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setAt()));
	connect(_atYLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setAt()));
	connect(_atZLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setAt()));

	//connect the attenuation
	connect(_constantAttenuationLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setAttenuation()));
	connect(_linearAttenuationLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setAttenuation()));
	connect(_quadraticAttenuationLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setAttenuation()));

	//connect the cutoff
	connect(_cutOffLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setCutOff()));

	//connect the exponent
	connect(_exponentLineEdit, SIGNAL(textEdited(const QString&)), this, SLOT(setExponent()));
}

//destructor
GRTParamLightSpot::~GRTParamLightSpot()
{
}

//fill all the widgets with values
void GRTParamLightSpot::fillWidgets(GRTILight* l)
{
	if(l->getType() == LT_SPOT)
	{
		_spotLight = (GRTSpotLight*)l;
		_atElement = _spotLight->getAtElement();
	}
	else
	{
		_atElement = (GRTSpotAt*)l;
		_spotLight = _atElement->getGRTSpotLightElement();
	}
	//position
	_positionXLineEdit->setText(QString::number(_spotLight->getTranslate().x));
	_positionYLineEdit->setText(QString::number(_spotLight->getTranslate().y));
	_positionZLineEdit->setText(QString::number(_spotLight->getTranslate().z));

	//at
	_atXLineEdit->setText(QString::number(_atElement->getTranslate().x));
	_atYLineEdit->setText(QString::number(_atElement->getTranslate().y));
	_atZLineEdit->setText(QString::number(_atElement->getTranslate().z));

	//direction
	RTVector3f dir = _atElement->getTranslate() - _spotLight->getTranslate();
	dir.normalize();
	_dirXLineEdit->setText(QString::number(dir.x));
	_dirYLineEdit->setText(QString::number(dir.y));
	_dirZLineEdit->setText(QString::number(dir.z));

	//attenuation
	_constantAttenuationLineEdit->setText(QString::number(_spotLight->getAttenuation().x));
	_linearAttenuationLineEdit->setText(QString::number(_spotLight->getAttenuation().y));
	_quadraticAttenuationLineEdit->setText(QString::number(_spotLight->getAttenuation().z));

	//cut off
	_cutOffLineEdit->setText(QString::number(_spotLight->getCutOff()));

	//exponent
	_exponentLineEdit->setText(QString::number(_spotLight->getExponent()));
}

//slot to set the position of the position widget
void GRTParamLightSpot::setPosition(float /*x*/, float /*y*/, float /*z*/)
{
	//set position
	_positionXLineEdit->setText(QString::number(_spotLight->getTranslate().x));
	_positionYLineEdit->setText(QString::number(_spotLight->getTranslate().y));
	_positionZLineEdit->setText(QString::number(_spotLight->getTranslate().z));

	//set at
	_atXLineEdit->setText(QString::number(_atElement->getTranslate().x));
	_atYLineEdit->setText(QString::number(_atElement->getTranslate().y));
	_atZLineEdit->setText(QString::number(_atElement->getTranslate().z));

	//set direction
	RTVector3f dir = _atElement->getTranslate() - _spotLight->getTranslate();
	dir.normalize();
	_dirXLineEdit->setText(QString::number(dir.x));
	_dirYLineEdit->setText(QString::number(dir.y));
	_dirZLineEdit->setText(QString::number(dir.z));
}

//set the light attenuation
void GRTParamLightSpot::setAttenuation()
{
	//get the selected object
	GRTMovable* m = SCENE.getSelectedObject();

	//if no object selected
	if(m == 0)
		return;

	//if the selected object is not a light
	if(m->getGRTMovableType() != MT_LIGHT)
		return;

	//if the light is not a point light
	GRTILight* l = (GRTILight*)m;
	if(l->getType() != LT_SPOT)
		return;

	//set the attenuation
	GRTSpotLight* p = (GRTSpotLight*)l;
	p->setAttenuation(RTVector3f(_constantAttenuationLineEdit->text().toFloat(),
		_linearAttenuationLineEdit->text().toFloat(),
		_quadraticAttenuationLineEdit->text().toFloat()));
	
}

//set exponent
void GRTParamLightSpot::setExponent()
{
	////get the selected object
	//GRTMovable* m = SCENE.getSelectedObject();

	////if no object selected
	//if(m == 0)
	//	return;

	////if the selected object is not a light
	//if(m->getGRTMovableType() != MT_LIGHT)
	//	return;

	////if the light is not a point light
	//ILight* l = (ILight*)m;
	//if(l->getType() != LT_SPOT)
	//	return;

	////set the attenuation
	//GRTSpotLight* p = (GRTSpotLight*)l;
	_spotLight->setExponent(_exponentLineEdit->text().toFloat());
}

//set cutoff
void GRTParamLightSpot::setCutOff()
{
	////get the selected object
	//GRTMovable* m = SCENE.getSelectedObject();

	////if no object selected
	//if(m == 0)
	//	return;

	////if the selected object is not a light
	//if(m->getGRTMovableType() != MT_LIGHT)
	//	return;

	////if the light is not a point light
	//ILight* l = (ILight*)m;
	//if(l->getType() != LT_SPOT)
	//	return;

	////set the attenuation
	//GRTSpotLight* p = (GRTSpotLight*)l;
	_spotLight->setCutOff(_cutOffLineEdit->text().toFloat());
}

//set the light position
void GRTParamLightSpot::setObjectPosition()
{
	_spotLight->setTranslate(RTVector3f(_positionXLineEdit->text().toFloat(),
		_positionYLineEdit->text().toFloat(),
		_positionZLineEdit->text().toFloat()));
}

//set the at position element
void GRTParamLightSpot::setAt()
{
	_atElement->setTranslate(RTVector3f(_atXLineEdit->text().toFloat(),
		_atYLineEdit->text().toFloat(),
		_atZLineEdit->text().toFloat()));
}